﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace WPFConverter
{
    public class BoolToVisibleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool)
            {
                return (bool)value == true ? Visibility.Visible : Visibility.Collapsed;
            }
            else if (value is bool?)
            {
                return (bool?)value == true ? Visibility.Visible : Visibility.Collapsed;
            }
            else
            {
                return Visibility.Visible;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Visibility)
            {
                Visibility v = (Visibility)value;
                switch (v)
                {
                    case Visibility.Visible:
                        return true;
                    case Visibility.Collapsed:
                        return false;
                    case Visibility.Hidden:
                        return null;
                    default:
                        return null;
                }
            }
            else
                return null;
        }
    }
}
